using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using System.Threading;
using System.Diagnostics;
using CodePlex.Resourceful;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class FilesControl2 : CodePlex.SpaceBlock.UI.Controls.BaseControl, ISupportsFileSystem
    {
        public event EventHandler SelectedFilesChanged;
        public event EventHandler TransferToRequested;

        private IFileSystem _fileSystem;
        private Path _folder;
        private readonly DataGridView2 _grid;

       
        public FilesControl2()
        {
            InitializeComponent();
           
            _grid = new DataGridView2();


            _grid.MouseDown += delegate
            {
                this.OnEnter(EventArgs.Empty);
            };

           

            UIUtils.FillWith(_gridHolder, _grid);

            DataGridViewColumn col = _grid.AddColumnText("Name", 100);
            col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            DataGridViewColumn col2 = _grid.AddColumnText("Size", 80);
            col2.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            DataGridViewColumn col3 = _grid.AddColumnText("DateModified", 100);
            col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            

            
            _grid.SortCompare += delegate(object sender, DataGridViewSortCompareEventArgs e)
            {
                if (e.Column == col2)
                {

                    long value1 = Utils.ParseSizeString(e.CellValue1 as string);
                    long value2 = Utils.ParseSizeString(e.CellValue2 as string);

                    e.SortResult = value1.CompareTo(value2);
                    e.Handled = true;
                }

                if (e.Column == col3)
                {

                    DateTime value1 = DateTime.Parse(e.CellValue1 as string);
                    DateTime value2 = DateTime.Parse(e.CellValue2 as string);

                    e.SortResult = value1.CompareTo(value2);
                    e.Handled = true;
                }
            };

       /*
            FormsUtils.ConfigureForDragDrop(_grid,delegate(Control control) {


                // folders
                TreeView treeview = control as TreeView;

                if (treeview != null)
                {
                    IFileSystemProvider fsProvider = treeview.Parent as IFileSystemProvider;

                    if (fsProvider != null)
                    {
                        return fsProvider.FileSystem != this.FileSystem;
                    }
                   
                }

             
                return true;
                
            },this.HandleDrop);
        */
            
            _grid.SelectionChanged += delegate
            {
                this.OnSelectedFilesChanged();
            };

        }

        public void Initialize(ActionManager manager, IFileSystem fileSystem) {

            _fileSystem = fileSystem;
            manager.AddAnchor(_grid);
        }

        public List<Path> SelectedFiles
        {
            get
            {
                return Utils.ToList(Utils.ConvertAll<DataGridViewRow, Path>(_grid.SelectedRows, delegate(DataGridViewRow row)
                {
                    return ((IFileItem)row.Tag).Path;
                }));
            }
        }

        public Path SelectedFolder
        {
            get
            {
                return _folder;
            }
            set
            {
                _folder = value;
                this.UpdateData();
            }
        }


        public IFileSystem FileSystem
        {
            get { return _fileSystem; }
        }










        private void OnSelectedFilesChanged()
        {
            if (this.SelectedFilesChanged != null)
            {
                this.SelectedFilesChanged(this, EventArgs.Empty);
            }
        }

        private void HandleDrop(Control sender, object[] dropValues)
        {
            if (_grid == sender)
            {
                return;
            }

            if (_folder == null)
            {
                return;
            }

            if (dropValues.Length > 0)
            {
                this.OnTransferToRequested();
                // this.UpdateData();
            }



        }

        private void UpdateData()
        {
            _grid.SuspendLayout();
            _grid.Rows.Clear();


            this.InBackground(delegate
            {
                IEnumerable<IFileItem> files = _fileSystem.GetFiles(_folder);


                this.InDisplay(delegate
                {

                    foreach (IFileItem file in files)
                    {
                        _grid.AddRow(file,
                             UIManager.Instance.GetSmallImage(file.Path, false),
                             file.Path.Name,
                           Utils.ComputeSizeString(file.Size),
                           file.LastModified.ToString()
                           );


                    }

                    _grid.ResumeLayout();

                    this.OnSelectedFilesChanged();

                   
                });
            });

        }

        private void OnTransferToRequested()
        {
            if (this.TransferToRequested != null)
            {
                this.TransferToRequested(this, EventArgs.Empty);
            }
        }

       
    }
}

